package com.lxb.blog.web;

import com.lxb.blog.service.BlogService;
import com.lxb.blog.service.TagService;
import com.lxb.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/tags")
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping
    public String TagShow(Model model,
                          @PageableDefault(size = 1,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                  Pageable pageable){
        model.addAttribute("tags",tagService.listTagTop(100));
        model.addAttribute("page",blogService.listBlog(pageable));
        model.addAttribute("activeTagId",null);
        return "tags";
    }

    @GetMapping("/{id}")
    public String tags(Model model,
                        @PageableDefault(size = 2,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        @PathVariable Integer id){

        BlogQuery blogQuery = new BlogQuery();
       // blogQuery.setT(id);
        model.addAttribute("tags",tagService.listTagTop(100));
        model.addAttribute("page",blogService.listBlog(id,pageable));
        model.addAttribute("activeTagId",id);
        return "tags";
    }
}
